/*
============================================================================
FileName	:	XQChatRosterManager.cpp
Author		:	liyake
Website		:	http://www.liyake.com
Create Data	:	2012-03-15 10:43
Change Data	:	2012-03-15 10:43
Detail		:	roster manager.
============================================================================
*/

#include <QFile>
#include <QXmppLogger.h>
#include "XQChatRosterManager.h"

using namespace XQChat;

extern QString g_qsDir;

XQChat::CXQChatRosterManager::CXQChatRosterManager( const QString& _jid, QObject* parent /*= 0*/ )
	: QObject(parent)
	, m_qsJid(_jid)
{
	loadNicks();
}

XQChat::CXQChatRosterManager::~CXQChatRosterManager( void )
{
	ClearRosters();
}

void XQChat::CXQChatRosterManager::ClearRosters()
{
	QMap<QString,CXQChatRoster*>::iterator iter = m_mapRosters.begin();
	while(iter!=m_mapRosters.end())
	{
		delete iter.value();
		++iter;
	}

	m_mapRosters.clear();
}

CXQChatRoster* XQChat::CXQChatRosterManager::getRosterByFullJid( const QString& jid )
{
	QMap<QString,CXQChatRoster*>::iterator iter = m_mapRosters.find(jid);
	if(iter!=m_mapRosters.end())
		return iter.value();

	return 0;
}

CXQChatRoster* XQChat::CXQChatRosterManager::getRosterByNick( const QString& nick )
{
	QList<CXQChatRoster*> listRoster = m_mapRosters.values();
	foreach(CXQChatRoster* pRoster, listRoster)
	{
		if(pRoster->getNick().compare(nick,Qt::CaseInsensitive)==0)
			return pRoster;
	}

	return 0;
}

bool XQChat::CXQChatRosterManager::addRoster( CXQChatRoster* pRoster )
{
	foreach(const QString& tmp, m_listBlocked)
	{
		if(pRoster->getFullJid().indexOf(tmp)>-1)
			return false;
	}

	if(getRosterByFullJid(pRoster->getFullJid()))		//if there already had a same roster, return false.
		return false;

	m_mapRosters[pRoster->getFullJid()]= pRoster;

	if(m_mapNicks.find(pRoster->getBareJid())!=m_mapNicks.end())
	{
		const stRoster& roster = m_mapNicks[pRoster->getBareJid()];
		pRoster->resetNameAndNick(roster.nick,roster.nick2);
		foreach(const QString& blocked,roster.blocks)
		{
			if(!blocked.isEmpty())
			{
				pRoster->addBlocked(blocked,false);
			}
		}
		pRoster->setStatu(roster.statu,false);
	}
	return true;
}

bool XQChat::CXQChatRosterManager::removeRoster( const QString& qsBareJid )
{
	if(!getRosterByFullJid(qsBareJid))		//if there already had a same roster, return false.
		return false;

	//此处是不是会造成内存泄漏呢？因为没有delete相应的roster，不能在此处清空
	return m_mapRosters.remove(qsBareJid);
}

QList<CXQChatRoster*> XQChat::CXQChatRosterManager::getRostersByBareJid( const QString& jid )
{
	QList<CXQChatRoster*> listRosters;
    foreach(CXQChatRoster* pRoster, m_mapRosters.values())
	{
		if( pRoster->getBareJid() == jid )
			listRosters.push_back(pRoster);
	}

	return listRosters;
}

QList<CXQChatRoster*> XQChat::CXQChatRosterManager::getRostersAll()
{
	return m_mapRosters.values();
}

void XQChat::CXQChatRosterManager::loadNicks()
{
	QXmppLogger::getLogger()->log(QXmppLogger::InformationMessage,
		QString("load nicks from:%1").arg(g_qsDir+"/"+m_qsJid));
	m_mapNicks.clear();
	QFile file(g_qsDir+"/"+m_qsJid);
	if(!file.open(QIODevice::ReadOnly))
		return;

	QString qsCmd = file.readLine();

	while(!qsCmd.isEmpty())
	{
		qsCmd = qsCmd.trimmed();
		QStringList listParam = qsCmd.split("&&");
		QString qsCurrentJid;
		if(listParam.size()>0)
		{
			QStringList listNick = listParam[0].trimmed().split("//");
			if(listNick.size()>1)
			{
				qsCurrentJid = listNick.at(0);
				m_mapNicks[listNick.at(0)].nick = listNick.at(1);
				m_mapNicks[listNick.at(0)].jid = listNick.at(0);
				m_mapNicks[qsCurrentJid].statu = 1;
				m_mapNicks[qsCurrentJid].nick2 = listNick.at(1);
			}
			if(listNick.size()>2)
			{
				int iStatu = listNick[2].trimmed().toInt();
				if(iStatu == 0)
				{
//					iStatu = 1;
				}
				if(iStatu>-1&&iStatu<3)
					m_mapNicks[qsCurrentJid].statu = iStatu;
			}
			if(listNick.size()>3)
			{
				m_mapNicks[qsCurrentJid].nick2 = listNick[3].trimmed();
			}
		}
		if(listParam.size()>1)
		{
			QStringList listBlocked = listParam[1].trimmed().split("//");
			foreach(const QString& blocked, listBlocked)
			{
				if(!blocked.isEmpty())
					m_mapNicks[qsCurrentJid].blocks.push_back(blocked);
			}
		}
		qsCmd = file.readLine();
	}

	return;
}

void XQChat::CXQChatRosterManager::saveNicks()
{
	QXmppLogger::getLogger()->log(QXmppLogger::InformationMessage,
		QString("save nicks to:%1").arg(g_qsDir+"/"+m_qsJid));

	QFile file(g_qsDir+"/"+m_qsJid);
	if(!file.open(QIODevice::WriteOnly))
		return;

	QMap<QString,stRoster>::iterator iter = m_mapNicks.begin();
	QString qsLine;
	while(iter!=m_mapNicks.end())
	{
		qsLine = iter.key() + "//" + iter.value().nick;
		qsLine += QString("//%1").arg(iter->statu);
		qsLine += QString("//%1").arg(iter->nick2);
		if(iter.value().blocks.size()>0)
		{
			qsLine += "&&";
			foreach(const QString& qsBlocked,iter.value().blocks)
			{
				if(!qsBlocked.isEmpty())
					qsLine = qsLine + qsBlocked + "//";
			}
			qsLine.remove(qsLine.size()-2,2);
		}
		qsLine += "\r\n";
		file.write(qsLine.toAscii());
		++iter;
	}
	file.close();
	return;
}

bool XQChat::CXQChatRosterManager::addBlocked( const QString& jid )
{
	QString jidBlock = jid.trimmed();
	foreach(const QString& tmp, m_listBlocked)
	{
		if(tmp.indexOf(jid)>-1)
			return false;
	}

	m_listBlocked.push_back(jidBlock);
	return true;
}

